package com.ruoyi.web.controller.hk;

import com.ruoyi.hk.domain.dto.DocMessageDto;
import com.ruoyi.hk.domain.vo.DocMessageVo;
import com.ruoyi.hk.domain.vo.Result;
import com.ruoyi.hk.domain.vo.UserConversationVO;
import com.ruoyi.hk.service.DocMessageService;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/chat")
public class DocController {

    @Autowired
    private DocMessageService docMessageService;

    @PostMapping("/send")
    public Result<DocMessageVo> sendMessage(@RequestBody DocMessageDto messageDTO) {
        try {
            Long userId = SecurityUtils.getUserId();
            DocMessageVo savedMessage = docMessageService.sendMessage(
                    userId,
                    messageDTO.getReceiverId(),
                    messageDTO.getContent(),
                    messageDTO.getMessageType()
            );

            return Result.success(savedMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("发送消息失败");
        }
    }

    @GetMapping("/history/{targetId}")
    public Result<List<DocMessageVo>> getChatHistory(@PathVariable Long targetId) {
        try {
            Long userId = SecurityUtils.getUserId();
            List<DocMessageVo> messages = docMessageService.getChatHistory(userId, targetId);
            return Result.success(messages);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取聊天记录失败");
        }
    }

    @GetMapping("/conversations")
    public Result<List<DocMessageVo>> getUserConversations() {
        try {
            Long userId = SecurityUtils.getUserId();
            List<DocMessageVo> conversations = docMessageService.getUserConversations(userId);
            return Result.success(conversations);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取会话列表失败");
        }
    }

    @GetMapping("/conversations/users")
    public Result<List<UserConversationVO>> getUserConversationsWithUserInfo() {
        try {
            Long userId = SecurityUtils.getUserId();
            List<UserConversationVO> conversations = docMessageService.getUserConversationsWithUserInfo(userId);
            return Result.success(conversations);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取会话列表失败");
        }
    }

    @GetMapping("/users/others")
    public Result<List<UserConversationVO>> getAllOtherUsers() {
        try {
            Long userId = SecurityUtils.getUserId();
            List<UserConversationVO> users = docMessageService.getAllOtherUsers(userId);
            return Result.success(users);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取用户列表失败");
        }
    }

    @PostMapping("/read/{targetId}")
    public Result<String> markMessagesAsRead(@PathVariable Long targetId) {
        try {
            Long userId = SecurityUtils.getUserId();
            docMessageService.markMessagesAsRead(userId, targetId);
            return Result.success("标记已读成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("标记已读失败");
        }
    }

    @GetMapping("/unread/{targetId}")
    public Result<Integer> getUnreadCount(@PathVariable Long targetId) {
        try {
            Long userId = SecurityUtils.getUserId();
            Integer count = docMessageService.getUnreadCount(userId, targetId);
            return Result.success(count);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取未读消息数失败");
        }
    }

    /**
     * 根据用户角色获取聊天用户列表
     */
    @GetMapping("/users/list")
    public Result<List<UserConversationVO>> getChatUserList() {
        Long userId = SecurityUtils.getUserId();
        List<UserConversationVO> users = docMessageService.getChatUserList(userId);
        return Result.success(users);
    }

    @GetMapping("/users")
    public Result<List<UserConversationVO>> getAllUsers() {
        try {
            Long userId = SecurityUtils.getUserId();
            List<UserConversationVO> users = docMessageService.getAllOtherUsers(userId);
            return Result.success(users);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取用户列表失败");
        }
    }
}

/*import com.ruoyi.hk.domain.dto.DocMessageDto;
import com.ruoyi.hk.domain.vo.DocMessageVo;
import com.ruoyi.hk.domain.vo.Result;
import com.ruoyi.hk.service.DocMessageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/api/chat")
public class DocController {

    @Autowired
    private DocMessageService docMessageService;

    @PostMapping("/send")
    public Result<DocMessageVo> sendMessage(@RequestBody DocMessageDto messageDTO, HttpSession session) {
        Long userId = (Long) session.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }

        try {
            DocMessageVo savedMessage = docMessageService.sendMessage(
                    userId,
                    messageDTO.getReceiverId(),
                    messageDTO.getContent(),
                    messageDTO.getMessageType()
            );

            return Result.success(savedMessage);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("发送消息失败");
        }
    }

    @GetMapping("/history/{targetId}")
    public Result<List<DocMessageVo>> getChatHistory(@PathVariable Long targetId, HttpSession session) {
        Long userId = (Long) session.getAttribute("userId");
        if (userId == null) {
            return Result.error("用户未登录");
        }

        try {
            List<DocMessageVo> messages = docMessageService.getChatHistory(userId, targetId);
            return Result.success(messages);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("获取聊天记录失败");
        }
    }
}*/
